#target "indesign"
(function () {
    if (app.documents.length === 0) { alert("请先打开一个文档。"); return; }
    var doc = app.activeDocument;

    // 标题段落样式
    var TITLE_STYLES = ["DANGER", "WARNING", "CAUTION", "NOTE"];
    // 下划线段落样式
    var UNDERLINE_STYLE = "下划线";

    var totalTitles = 0;
    var okCount = 0;
    var bad = [];

    app.doScript(function () {
        var paras = doc.stories.everyItem().paragraphs.everyItem().getElements();

        for (var i = 0; i < paras.length; i++) {
            var para = paras[i];
            if (!para.isValid) continue;

            var pStyleName = "";
            try { pStyleName = String(para.appliedParagraphStyle.name); } catch(_){}
            if (!isTitleStyle(pStyleName)) continue;

            totalTitles++;

            // 向后查找直到遇到 下划线段 或者另一个标题段
            var foundUnderline = false;
            for (var j = i + 1; j < paras.length; j++) {
                var next = paras[j];
                if (!next.isValid) break;

                var nStyle = "";
                try { nStyle = String(next.appliedParagraphStyle.name); } catch(_){}

                if (isTitleStyle(nStyle)) {
                    // 遇到下一个标题了，还没找到下划线
                    break;
                }
                if (nStyle === UNDERLINE_STYLE) {
                    foundUnderline = true;
                    break;
                }
            }

            if (foundUnderline) {
                okCount++;
            } else {
                bad.push({
                    pageNum: getPageNumberSafe(para),
                    contents: safePreview(para.contents, 80)
                });
            }
        }
    }, ScriptLanguage.JAVASCRIPT, undefined, UndoModes.ENTIRE_SCRIPT, "检查警告标题的下划线");

    // 输出结果
    var msg = [];
    msg.push("警告标题总数：" + totalTitles);
    msg.push("合格：" + okCount);
    msg.push("未配下划线：" + (totalTitles - okCount));
    msg.push("");
    if (bad.length > 0) {
        msg.push("以下标题未找到对应下划线：");
        for (var k = 0; k < bad.length; k++) {
            msg.push("P" + bad[k].pageNum + "：" + bad[k].contents);
        }
    } else {
        msg.push("全部通过 ✅");
    }

    var w = new Window("dialog", "检查结果");
    w.orientation = "column";
    w.alignChildren = ["fill", "top"];
    var et = w.add("edittext", undefined, msg.join("\r"), {multiline:true, scrolling:true});
    et.preferredSize = [700, 400];
    var btns = w.add("group"); btns.alignment = ["right","top"];
    btns.add("button", undefined, "确定", {name:"ok"});
    w.center(); w.show();

    /* ===== 工具函数 ===== */
    function isTitleStyle(name) {
        for (var i = 0; i < TITLE_STYLES.length; i++) {
            if (name === TITLE_STYLES[i]) return true;
        }
        return false;
    }
    function safePreview(s, maxLen) {
        s = (s == null) ? "" : String(s);
        s = s.replace(/\r/g, " / ");
        if (s.length > maxLen) s = s.substring(0, maxLen - 1) + "…";
        return s;
    }
    function getPageNumberSafe(para) {
        try {
            var ip = para.insertionPoints[0];
            if (!ip) return "?";
            var tf = ip.parentTextFrames[0];
            if (!tf) return "?";
            var pg = tf.parentPage;
            if (!pg || !pg.isValid) return "–";
            return pg.name;
        } catch (_){ return "–"; }
    }
})();
